import React, { useState, useReducer } from "react";
import { Route, Switch, Redirect, withRouter } from "react-router-dom";
import { Layout, BackTop, message } from "antd";
import routes from "@/routes";
import menus from "@/routes/menus";
import avatar from "@/assets/images/user.png";

import store from "../store";
import "@/style/layout.less";

import AppHeader from "./AppHeader.jsx";
import AppAside from "./AppAside.jsx";
import AppFooter from "./AppFooter.jsx";

const { Content } = Layout;

const MENU_TOGGLE = "menuToggle";

const reducer = (state, action) => {
  switch (action.type) {
    case MENU_TOGGLE:
      return { ...state, menuToggle: !state.menuToggle };
    default:
      return state;
  }
};

const getMenu = menu => {
  let auth = store.getState().user_auth;
  if (auth.length > 0) {
    return auth;
  } else {
    var shuju = [{ "title": "首页", "icon": "DashboardFilled", "path": "/", "isShow": true, "id": "M000001", "parentId": "0", "child": [] }, { "title": "产品", "icon": "AppstoreOutlined", "path": "/product", "isShow": true, "id": "M000002", "parentId": "0", "child": [{ "title": "缺货清单", "icon": "ContainerOutlined", "path": "/bo/list", "isShow": true, "id": "M000003", "parentId": "M000002", "child": [] }, { "title": "产品信息", "icon": "AppstoreOutlined", "path": "/product/list", "isShow": true, "id": "M000100", "parentId": "M000002", "child": [] }] }, { "title": "订单", "icon": "ShoppingOutlined", "path": "/order", "isShow": true, "id": "M000018", "parentId": "0", "child": [{ "title": "询价", "icon": "MessageOutlined", "path": "/quote/list", "isShow": true, "id": "M000111", "parentId": "M000018", "child": [] }, { "title": "在线订单", "icon": "ShoppingOutlined", "path": "/shopOrder/list", "isShow": true, "id": "M000019", "parentId": "M000018", "child": [] }, { "title": "订单列表", "icon": "ShoppingOutlined", "path": "/order/list", "isShow": true, "id": "M000122", "parentId": "M000018", "child": [] }, { "title": "订单明细", "icon": "ShoppingOutlined", "path": "/orderDetail/list", "isShow": true, "id": "M000145", "parentId": "M000018", "child": [] }, { "title": "发货明细", "icon": "ShoppingOutlined", "path": "/shipDetail/list", "isShow": true, "id": "M000149", "parentId": "M000018", "child": [] }, { "title": "发货管理", "icon": "NodeIndexOutlined", "path": "/ship", "isShow": true, "id": "M000152", "parentId": "M000018", "child": [{ "title": "免费样审核", "icon": null, "path": "/order/freeSample", "isShow": true, "id": "M000157", "parentId": "M000152", "child": [] }, { "title": "待发货", "icon": null, "path": "/waitingShip/list", "isShow": true, "id": "M000156", "parentId": "M000152", "child": [] }, { "title": "期货", "icon": null, "path": "/backOrder/list", "isShow": true, "id": "M000161", "parentId": "M000152", "child": [] }, { "title": "核对发货", "icon": null, "path": "/confirmShip/list", "isShow": true, "id": "M000167", "parentId": "M000152", "child": [] }, { "title": "发货记录", "icon": null, "path": "/ship/list", "isShow": true, "id": "M000179", "parentId": "M000152", "child": [] }] }, { "title": "服务", "icon": "CustomerServiceOutlined", "path": "/customAntigen", "isShow": true, "id": "M000187", "parentId": "M000018", "child": [{ "title": "代加工", "icon": null, "path": "/customAntigen/list", "isShow": true, "id": "M000188", "parentId": "M000187", "child": [] }, { "title": "生成代检测", "icon": null, "path": "/customTesting/waitingTestList", "isShow": true, "id": "M000343", "parentId": "M000187", "child": [] }, { "title": "代检测", "icon": null, "path": "/customTesting/list", "isShow": true, "id": "M000346", "parentId": "M000187", "child": [] }] }] }, { "title": "财务", "icon": "MoneyCollectOutlined", "path": "/bill", "isShow": true, "id": "M000201", "parentId": "0", "child": [{ "title": "现结代开账单", "icon": "ImportOutlined", "path": "/waitingBill/list", "isShow": true, "id": "M000202", "parentId": "M000201", "child": [] }, { "title": "账单列表", "icon": "AccountBookOutlined", "path": "/bill/list", "isShow": true, "id": "M000208", "parentId": "M000201", "child": [] }, { "title": "付款列表", "icon": "TransactionOutlined", "path": "/payment/list", "isShow": true, "id": "M000224", "parentId": "M000201", "child": [] }, { "title": "发票", "icon": "GroupOutlined", "path": "/invoice/list", "isShow": true, "id": "M000227", "parentId": "M000201", "child": [] }, { "title": "用户存款", "icon": "WalletOutlined", "path": "/creditMemo/list", "isShow": true, "id": "M000230", "parentId": "M000201", "child": [] }, { "title": "统计账单", "icon": "PieChartOutlined", "path": "/stiatic/billStiatic", "isShow": true, "id": "M000342", "parentId": "M000201", "child": [] }] }, { "title": "代理商", "icon": "TeamOutlined", "path": "/partners", "isShow": true, "id": "M000026", "parentId": "0", "child": [{ "title": "代理商信息", "icon": "ShoppingOutlined", "path": "/partners/list", "isShow": true, "id": "M000027", "parentId": "M000026", "child": [] }, { "title": "AM代理商", "icon": "ShoppingOutlined", "path": "/partners/amPartnerList", "isShow": true, "id": "M000540", "parentId": "M000026", "child": [] }, { "title": "代理商销售情况", "icon": "ShoppingOutlined", "path": "/partnerSale/list", "isShow": true, "id": "M000237", "parentId": "M000026", "child": [] }, { "title": "代理商任务完成情况", "icon": "ShoppingOutlined", "path": "/partnerSale/detailList", "isShow": true, "id": "M000286", "parentId": "M000026", "child": [] }, { "title": "内部任务完成情况", "icon": "ShoppingOutlined", "path": "/employeeSale/list", "isShow": true, "id": "M000240", "parentId": "M000026", "child": [] }] }, { "title": "用户", "icon": "TeamOutlined", "path": "/customer", "isShow": true, "id": "M000051", "parentId": "0", "child": [{ "title": "客户信息", "icon": "UserOutlined", "path": "/customer/list", "isShow": true, "id": "M000052", "parentId": "M000051", "child": [] }, { "title": "机构信息", "icon": "ApartmentOutlined", "path": "/institution/list", "isShow": true, "id": "M000064", "parentId": "M000051", "child": [] }, { "title": "终端用户信息", "icon": "ContainerOutlined", "path": "/leads/list", "isShow": true, "id": "M000071", "parentId": "M000051", "child": [] }, { "title": "待处理投诉", "icon": "AliwangwangOutlined", "path": "/complain/waitProcessList", "isShow": true, "id": "M000378", "parentId": "M000051", "child": [] }, { "title": "驳回投诉", "icon": "AliwangwangOutlined", "path": "/complain/notPassList", "isShow": true, "id": "M000533", "parentId": "M000051", "child": [] }, { "title": "售后与投诉", "icon": "AliwangwangOutlined", "path": "/complain/list", "isShow": true, "id": "M000242", "parentId": "M000051", "child": [] }, { "title": "换货列表", "icon": "AliwangwangOutlined", "path": "/complain/reshipList", "isShow": true, "id": "M000297", "parentId": "M000051", "child": [] }, { "title": "退款列表", "icon": "AliwangwangOutlined", "path": "/complain/refundList", "isShow": true, "id": "M000298", "parentId": "M000051", "child": [] }, { "title": "免费样反馈", "icon": "ContainerOutlined", "path": "/feedback/list", "isShow": true, "id": "M000261", "parentId": "M000051", "child": [] }] }, { "title": "市场", "icon": "TeamOutlined", "path": "/market", "isShow": true, "id": "M000078", "parentId": "0", "child": [{ "title": "文献回馈", "icon": "BookOutlined", "path": "/market/citationReward/list", "isShow": true, "id": "M000079", "parentId": "M000078", "child": [] }] }, { "title": "设置", "icon": "SettingOutlined", "path": "/employee", "isShow": true, "id": "M000087", "parentId": "0", "child": [{ "title": "员工列表", "icon": "UserOutlined", "path": "/employee/list", "isShow": true, "id": "M000088", "parentId": "M000087", "child": [] }, { "title": "菜单管理", "icon": "MenuOutlined", "path": "/menuList/list", "isShow": true, "id": "M000265", "parentId": "M000087", "child": [] }, { "title": "角色管理", "icon": "MenuOutlined", "path": "/role/list", "isShow": true, "id": "M000275", "parentId": "M000087", "child": [] }] }, { "title": "统计", "icon": "AreaChartOutlined", "path": "/stiatic", "isShow": true, "id": "M000271", "parentId": "0", "child": [{ "title": "销售种类统计", "icon": "PieChartOutlined", "path": "/stiatic/saleStiatic", "isShow": true, "id": "M000272", "parentId": "M000271", "child": [] }, { "title": "产品分类统计", "icon": "PieChartOutlined", "path": "/stiatic/productDetailStiatic", "isShow": true, "id": "M000299", "parentId": "M000271", "child": [] }] }, { "title": "积分商城", "icon": "AreaChartOutlined", "path": "/mall", "isShow": true, "id": "M000300", "parentId": "0", "child": [{ "title": "客户管理", "icon": "UserOutlined", "path": "/mall/mallCustomer/list", "isShow": true, "id": "M000301", "parentId": "M000300", "child": [] }, { "title": "积分日志", "icon": "PieChartOutlined", "path": "/mall/pointLog/list", "isShow": true, "id": "M000305", "parentId": "M000300", "child": [] }, { "title": "供应商管理", "icon": "PieChartOutlined", "path": "/mall/vendor/list", "isShow": true, "id": "M000307", "parentId": "M000300", "child": [] }, { "title": "产品管理", "icon": "PieChartOutlined", "path": "/mall/mallProduct/list", "isShow": true, "id": "M000312", "parentId": "M000300", "child": [] }, { "title": "采购管理", "icon": "PieChartOutlined", "path": "/mall/po/list", "isShow": true, "id": "M000317", "parentId": "M000300", "child": [] }, { "title": "礼品分发", "icon": "PieChartOutlined", "path": "/mall/export/list", "isShow": true, "id": "M000326", "parentId": "M000300", "child": [] }, { "title": "兑换列表", "icon": "PieChartOutlined", "path": "/mall/redeem/list", "isShow": true, "id": "M000332", "parentId": "M000300", "child": [] }, { "title": "问卷列表", "icon": "PieChartOutlined", "path": "/mall/questionnaire/list", "isShow": true, "id": "M000551", "parentId": "M000300", "child": [] }] }, { "title": "网站相关", "icon": "AreaChartOutlined", "path": "/website", "isShow": true, "id": "M000535", "parentId": "0", "child": [{ "title": "宣传册", "icon": "AreaChartOutlined", "path": "/website/flyer/list", "isShow": true, "id": "M000536", "parentId": "M000535", "child": [] }] }, { "title": "个人中心", "icon": "TeamOutlined", "path": "/center", "isShow": true, "id": "M000541", "parentId": "0", "child": [{ "title": "代理商任务完成情况", "icon": "ShoppingOutlined", "path": "/partnerSale/myDetailList", "isShow": true, "id": "M000542", "parentId": "M000541", "child": [] }, { "title": "内部任务完成情况", "icon": "ShoppingOutlined", "path": "/employeeSale/myList", "isShow": true, "id": "M000543", "parentId": "M000541", "child": [] }, { "title": "代理商销售情况", "icon": "ShoppingOutlined", "path": "/partnerSale/myList", "isShow": true, "id": "M000544", "parentId": "M000541", "child": [] }, { "title": "销售种类统计", "icon": "PieChartOutlined", "path": "/stiatic/saleMyStiatic", "isShow": true, "id": "M000550", "parentId": "M000541", "child": [] }] }];
    console.log(menu, "看看menu", auth);
    // newMenu = menu.filter(res => res.auth && res.auth.indexOf(auth) !== -1);
    return shuju;
  }
};

const DefaultLayout = props => {
  const [menu] = useState(prevState => {
    if (!localStorage.getItem("USER_TOKEN")) {
      props.history.push("/login");
      return [];
    } else {
      console.log(menus, "menusmenus");
      return getMenu(menus);
    }
  });
  const [state, dispatch] = useReducer(reducer, { menuToggle: false });
  let { auth } = store.getState().user_auth ? store.getState().user_auth : "";
  const menuClick = () => {
    dispatch({ type: "menuToggle" });
  };

  const loginOut = () => {
    localStorage.clear();
    props.history.push("/login");
    message.success("登出成功!");
  };

  return (
    <Layout className="app">
      <BackTop />
      <AppAside menuToggle={state.menuToggle} menu={menu} />
      <Layout style={{ minHeight: "100vh" }}>
        <AppHeader menuToggle={state.menuToggle} menuClick={menuClick} avatar={avatar} loginOut={loginOut} />
        <Content className="content">
          <Switch>
            {routes.map(item => {
              return (
                <Route
                  key={item.path}
                  path={item.path}
                  exact={item.exact}
                  render={props =>
                    !auth ? (
                      <item.component {...props} />
                    ) : item.auth && item.auth.indexOf(auth) !== -1 ? (
                      <item.component {...props} />
                    ) : (
                      // 这里也可以跳转到 403 页面
                      <Redirect to="/404" {...props} />
                    )
                  }
                />
              );
            })}
            <Redirect to="/404" />
          </Switch>
        </Content>
        <AppFooter />
      </Layout>
    </Layout>
  );
};

export default withRouter(DefaultLayout);
